//
//  CollectionDetailTableViewCell.m
//  Tang
//
//  Created by 邓芮 on 15/12/7.
//  Copyright © 2015年 352324497@qq.com. All rights reserved.
//

#import "CollectionDetailTableViewCell.h"

@interface CollectionDetailTableViewCell ()
// 最上面的view
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *time;
@property (nonatomic, strong) UIButton *attention;

// 图片
@property (nonatomic, strong) UIImageView *icon;

// 描述
@property (nonatomic, strong) UILabel *content;

// 标识
@property (nonatomic, strong) UIView *tagView;
@property (nonatomic, strong) UIImageView *littleView;
@property (nonatomic, strong) UIView *view;

// 最下面点击button
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) UIView *ver1;

@property (nonatomic, strong) UILabel *label;

@end

@implementation CollectionDetailTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        CGFloat topHeight = 80 * KWidth / 414;
        CGFloat x = 20 * KWidth / 414;
        CGFloat headerWidth = 40 * KWidth / 414;
        self.selectionStyle = NO;
        self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWidth, topHeight)];
        self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, x, headerWidth, headerWidth)];
        self.headerImageView.layer.cornerRadius = headerWidth / 2;
        self.headerImageView.layer.masksToBounds = YES;
        [self.topView addSubview:self.headerImageView];
        CGFloat nameX = x + headerWidth + 10 * KWidth / 414;
        CGFloat attentionWidth = 80 * KWidth / 414;
        CGFloat nameWidth = KWidth - 40 * KWidth / 414 - attentionWidth;
        self.name = [[UILabel alloc]initWithFrame:CGRectMake(nameX, x, nameWidth, x)];
        self.name.textColor = [UIColor lightGrayColor];
        self.name.font = [UIFont systemFontOfSize:16 * KWidth / 414];
        [self.topView addSubview:self.name];
        
        self.time = [[UILabel alloc]initWithFrame:CGRectMake(nameX, x +self.name.height, nameWidth, x)];
        self.time.textColor = [UIColor grayColor];
        self.time.font = [UIFont systemFontOfSize:15 * KWidth / 414];
        [self.topView addSubview:self.time];
        
        [self.contentView addSubview:self.topView];
        
        self.icon = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.topView.height, KWidth, 0)];
        [self.contentView addSubview:self.icon];
        
        self.content = [[UILabel alloc]initWithFrame:CGRectMake(x, self.topView.height + self.icon.height, KWidth - 2 * x, 0)];
        self.content.numberOfLines = 0;
        self.content.textColor = [UIColor blackColor];
        self.content.font = [UIFont systemFontOfSize: 18 * KWidth / 414];
        [self.contentView addSubview:self.content];
        
        self.tagView = [[UIView alloc]initWithFrame:CGRectMake(0, self.topView.height + self.icon.height + self.content.height, KWidth, 40 * KWidth / 414)];
        CGFloat littleWidth = 20 * KWidth / 414;
        self.littleView = [[UIImageView alloc]initWithFrame:CGRectMake(x, 10 * KWidth / 414, littleWidth, littleWidth)];
        self.littleView.image = [UIImage imageNamed:@"tag"];
        [self.tagView addSubview:self.littleView];
        
        self.view = [[UIView alloc]initWithFrame:CGRectMake(x + self.littleView.width, 10 * KWidth / 414, KWidth - x - self.littleView.width, 20 * KWidth / 414)];
        [self.tagView addSubview:self.view];
        
        [self.contentView addSubview:self.tagView];
        
        CGFloat bottomHeight = 40 * KWidth / 414;
        self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.topView.height + self.icon.height + self.content.height, KWidth, bottomHeight)];
        CGFloat btnWidth = (KWidth - 2 * KWidth / 414) / 2;
        CGFloat y = 7 * KWidth / 414;
        CGFloat bW = btnWidth / 2 + 10 * KWidth / 414;
        CGFloat bx = (btnWidth - bW) / 2;
        self.comment = [[DIYButton alloc]initWithFrame:CGRectMake(bx, y, bW, bottomHeight - 2 * y)];
        self.comment.iconImageView.image = [UIImage imageNamed:@"comment"];
        self.comment.textLabel.text = @"评论";
        self.comment.textLabel.font = [UIFont systemFontOfSize:18 * KWidth / 414];
        [self.bottomView addSubview:self.comment];
        
        CGFloat verX = 10 * KWidth / 414;
        CGFloat verW = 2 * KWidth / 414;
        CGFloat verH = (bottomHeight - 2 * verX);
        UIView *ver1 = [[UIView alloc]initWithFrame:CGRectMake(btnWidth, verX, verW, verH)];
        ver1.backgroundColor = [UIColor lightGrayColor];
        [self.bottomView addSubview:ver1];
        self.ver1 = ver1;
        
        self.like = [[DIYButton alloc]initWithFrame:CGRectMake(btnWidth + 2 * KWidth / 414 + bx, y, bW, bottomHeight - 2 * y)];
        self.like.iconImageView.image = [UIImage imageNamed:@"like"];
        self.like.selectIconImageView.image = [UIImage imageNamed:@"islike"];
        self.like.textLabel.text = @"喜欢";
        self.like.textLabel.font = [UIFont systemFontOfSize:18 * KWidth / 414];
        [self.bottomView addSubview:self.like];
        
        [self.contentView addSubview:self.bottomView];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, self.bottomView.y + self.bottomView.height, KWidth, 10 * KWidth / 414)];
        label.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1];
        [self.contentView addSubview:label];
        self.label = label;
    }
    return self;
}

+ (CGFloat)heightForCell:(CollectionDetailCellModel *)model tag:(NSInteger)tag;
{
    CGFloat conHeight = 0;
    CGFloat topViewHeight = 80 * KWidth / 414;
    if (model.content != nil)
    {
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
        [style setLineSpacing:6 * KWidth / 414];
        conHeight = [model.content boundingRectWithSize:CGSizeMake(KWidth - 40 * KWidth / 414, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18 * KWidth / 414], NSParagraphStyleAttributeName:style} context:nil].size.height + 20 * KWidth / 414;
        
        
    }
    PictureModel *picModel = model.picArray[0];
    CGFloat iconHeight = KWidth * picModel.h / picModel.w;
    CGFloat tagHeight = 40 * KWidth / 414;
    CGFloat bomHeight = 0;

    if (tag == 2000)
    {
       bomHeight = 40 * KWidth / 414;
    }
    
    return topViewHeight + conHeight + iconHeight + tagHeight + bomHeight + 10 * KWidth / 414;
}

- (void)setModel:(CollectionDetailCellModel *)model tag:(NSInteger)tag
{
    _model = model;
    self.label.height = 0;
    [self.view removeFromSuperview];
    self.view = [[UIView alloc]initWithFrame:CGRectMake(40 * KWidth / 414, 10 * KWidth / 414, KWidth - 40 * KWidth / 414, 20 * KWidth / 414)];
    [self.tagView addSubview:self.view];
    
    UserModel *userModel = [model.authorDic objectForKey:@"authorInfo"];
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:userModel.avatar] placeholderImage:[UIImage imageNamed:@"happy"]];
    self.name.text = userModel.username;
    self.time.text = model.datestr;
    
    PictureModel *picModel = model.picArray[0];
    [self.icon sd_setImageWithURL:[NSURL URLWithString:picModel.p]placeholderImage:[UIImage imageNamed:@"PC"]];
    self.icon.frame = CGRectMake(0, self.topView.height, KWidth, KWidth * picModel.h / picModel.w);
    
    if (model.content != nil)
    {
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:model.content];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
        style.lineSpacing = 6 * KWidth / 414;
        self.content.numberOfLines = 0;
        [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, model.content.length)];
        CGRect bounds = [model.content boundingRectWithSize:CGSizeMake(KWidth - 40 * KWidth / 414, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.content.font, NSParagraphStyleAttributeName:style} context:nil];
        self.content.attributedText = attStr;
        self.content.frame = CGRectMake(20 * KWidth / 414, self.icon.height + self.topView.height, KWidth - 40 * KWidth / 414, bounds.size.height + 20 * KWidth / 414);
    }
    
    self.tagView.frame = CGRectMake(0, self.topView.height + self.icon.height + self.content.height, KWidth, 40 * KWidth / 414);

    CGFloat width = 0;
    int a = 0;
    if (model.tagsArray.count >= 8)
    {
        a = 8;
    }
    else
    {
        a = (int)model.tagsArray.count;
    }
    for (int i = 0; i < a; i++)
    {
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:14 * KWidth / 414];
        TagModel *tag = model.tagsArray[i];
        
        CGFloat laW = [CallTool widthForNstringWithString:tag.name fontSize:14 * KWidth / 414] + 5 * KWidth / 414;
        label.text = tag.name;
        label.frame = CGRectMake(0 * KWidth / 414 + width, 0, laW, 20 * KWidth / 414);
        width = laW + width;
        
        label.textColor = [UIColor redColor];
        label.textAlignment = NSTextAlignmentCenter;

        [self.view addSubview:label];
    }
    
    //self.like.textLabel.text = model.likes;
    
    if (tag == 1000)
    {
        self.comment.frame = CGRectZero;
        self.like.frame = CGRectZero;
        self.bottomView.frame = CGRectZero;
        self.ver1.frame = CGRectZero;
        self.label.frame = CGRectMake(0, self.topView.height + self.content.height + self.tagView.height + self.icon.height, KWidth, 10 * KWidth / 414);

    }
    else if(tag == 2000)
    {
//        self.comment.textLabel.text = model.comments;
        self.bottomView.frame = CGRectMake(0, self.topView.height + self.icon.height + self.content.height + self.tagView.height, KWidth, 40 * KWidth / 414);
        self.label.frame = CGRectMake(0, self.bottomView.y + self.bottomView.height, KWidth, 10 * KWidth / 414);
        
        if ([[CoreDataMan shareIntance]findEveryWithId:model.Id])
        {
            self.like.selected = YES;
        }
        else
        {
            self.like.selected = NO;
        }
    }

  
 
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
